package com.zigong.app.ui.adapter.holder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.jiongbull.jlog.JLog;
import com.luck.picture.lib.PictureExternalPreviewActivity;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.ruihang.generalibrary.utils.ListUtils;
import com.ruihang.generalibrary.utils.UrlUtils;
import com.zigong.app.R;
import com.zigong.app.application.Constances;
import com.zigong.app.beans.ILiveDy;
import com.zigong.app.beans.ImageInfo;
import com.zigong.app.ui.activities.NewesDetailActivity;
import com.zigong.app.ui.utils.GlideUtil;
import com.zigong.app.ui.utils.ItemImagTarget;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by rh008 on 2017/7/18.
 */

public class LiveRoomDynamicHolder extends BaseAbsHolder<ILiveDy> {
    @BindView(R.id.item_live_room_time)
    protected CustomFontTextView time;
    @BindView(R.id.item_live_room_content)
    protected CustomFontTextView content;
    @BindView(R.id.item_live_room_dv_top)
    protected View up;
    @BindView(R.id.item_live_room_divider)
    protected View top;
    @BindView(R.id.item_live_room_dv_bottom)
    protected View bottom;
    @BindView(R.id.item_live_room_imgs_list)
    protected ViewGroup imgListLay;
    @BindView(R.id.item_live_room_imgs)
    protected ViewGroup imgLay;

    @BindView(R.id.item_live_room_imgs1)
    protected ViewGroup imgLay1;
    @BindView(R.id.item_live_room_imgs2)
    protected ViewGroup imgLay2;

    @BindView(R.id.item_live_room_play)
    protected ImageView playBtn;

    @BindView(R.id.item_live_room_img1)
    protected ImageView img1;
    @BindView(R.id.item_live_room_img2)
    protected ImageView img2;
    @BindView(R.id.item_live_room_img3)
    protected ImageView img3;

    @BindView(R.id.item_live_room_img4)
    protected ImageView img4;
    @BindView(R.id.item_live_room_img5)
    protected ImageView img5;
    @BindView(R.id.item_live_room_img6)
    protected ImageView img6;

    @BindView(R.id.item_live_room_img7)
    protected ImageView img7;
    @BindView(R.id.item_live_room_img8)
    protected ImageView img8;
    @BindView(R.id.item_live_room_img9)
    protected ImageView img9;


    @BindView(R.id.iv_item_live_room_single)
    protected ImageView ivSingle;
    //    @BindView(R.id.iv_item_live_room_only)
//    protected ImageView ivOnly;
    private ImageView[] imageViews;

    private boolean isVideo;

    private ArrayList<LocalMedia> imgsTemp;

    private ItemImagTarget singleTarget;


    public LiveRoomDynamicHolder(Context context, View itemView) {
        super(context, itemView);
        imageViews = new ImageView[]{img1, img2, img3, img4, img5, img6, img7, img8, img9};
        for (int i = 0; i < imageViews.length; i++) {
            imageViews[i].setTag(R.id.item_news_mode_tag, i);
        }
//        ivOnly.setTag(R.id.item_news_mode_tag, 0);
        ivSingle.setTag(R.id.item_news_mode_tag, 0);
        singleTarget = new ItemImagTarget(ivSingle);
        content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (content.getWidth() > 0) {
                    singleTarget.setMxW(content.getWidth());
                    content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            }
        });
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(final ILiveDy dynamic) {
        JLog.e(getAdapterPosition() + "  " + dynamic);
        if (dynamic.getDataType() == 0) {
            top.setVisibility(View.GONE);
            up.setVisibility(View.GONE);
        } else {
            top.setVisibility(View.VISIBLE);
            up.setVisibility(View.VISIBLE);
        }
        int m = 0;

        if (TextUtils.isEmpty(dynamic.getDyVideoUrl())) {
            isVideo = false;
            ivSingle.setVisibility(View.GONE);
            playBtn.setVisibility(View.GONE);
            m = 1;
            if (ListUtils.isListValued(dynamic.getContentImage())) {
                m = 11;
                if (dynamic.getContentImage().size() == 1) {
                    m = 111;
                    if (!setOnlyImage(dynamic.getContentImage().get(0))) {
                        JLog.e("qqsq  pos=" + getAdapterPosition() + "   sigle url is null");
                        return;
                    }
//                    ivOnly.setVisibility(View.VISIBLE);
                    ivSingle.setVisibility(View.VISIBLE);
                    imgListLay.setVisibility(View.GONE);
//                    GlideUtil.getGlid(context, dynamic.getContentImage().get(0).getImageUrl()).into(singleTarget);
                    GlideUtil.getGlideWithMid169DefNoFit(context, dynamic.getContentImage().get(0).getImageUrl()).into(singleTarget);
                } else {
                    m = 112;
//                    ivOnly.setVisibility(View.GONE);
                    int size = dynamic.getContentImage().size();
                    imgListLay.setVisibility(View.VISIBLE);
                    if (size > 3) {
                        imgLay1.setVisibility(View.VISIBLE);
                        imgLay2.setVisibility(size > 6 ? View.VISIBLE : View.GONE);
                    } else {
                        imgLay1.setVisibility(View.GONE);
                        imgLay2.setVisibility(View.GONE);
                    }
                    setImages(dynamic.getContentImage());
                }

            } else {
                m = 12;
                isVideo = true;
//                ivOnly.setVisibility(View.GONE);
                ivSingle.setVisibility(View.GONE);
                imgListLay.setVisibility(View.GONE);
                playBtn.setVisibility(View.GONE);
            }
        } else {
            m = 2;
            ivSingle.setVisibility(View.VISIBLE);
            playBtn.setVisibility(View.VISIBLE);
//            ivOnly.setVisibility(View.GONE);
            imgListLay.setVisibility(View.GONE);
            if (ListUtils.isListValued(dynamic.getContentImage())) {
                m = 21;
//                GlideUtil.loadImgLarge169DefNotFit(context, dynamic.getContentImage().get(0).getImageUrl(), ivSingle);
                GlideUtil.getGlideWithMid169DefNoFit(context, dynamic.getContentImage().get(0).getImageUrl()).into(singleTarget);
            }
        }

//        time.setText(DateUtils.timeAgo(DateUtils.getDataDate(dynamic)));
        time.setText(DateUtils.formatDate(DateUtils.getDataDate(dynamic), DateUtils.PATTERN_OUT_MONTH_TIME));
        if (TextUtils.isEmpty(dynamic.getContentText())) {
            content.setVisibility(View.GONE);
        } else {
            content.setVisibility(View.VISIBLE);
            content.setText(dynamic.getContentText());
        }
        JLog.e("qqsq   m=" + m + "   " + getAdapterPosition() + "   " + dynamic);

    }

    @Override
    public void setItemMode(int mode) {
//        JLog.e("qqsq mode=" + mode + "    pos=" + getAdapterPosition());
        int x = 0;
        if (mode == ITEMMODE_LAST) {
            x = 1;
            bottom.setVisibility(View.GONE);
            top.setVisibility(View.VISIBLE);
            up.setVisibility(View.VISIBLE);
        } else {
            x = 2;
            if (mode == ITEMMODE_SINGLE) {
                x = 21;
                bottom.setVisibility(View.GONE);
                top.setVisibility(View.GONE);
                up.setVisibility(View.GONE);
            } else {
                x = 22;
                bottom.setVisibility(View.VISIBLE);
                if (mode == ITEMMODE_FIRST) {
                    x = 221;
                    top.setVisibility(View.GONE);
                    up.setVisibility(View.GONE);
                } else {
                    x = 222;
                    top.setVisibility(View.VISIBLE);
                    up.setVisibility(View.VISIBLE);
                }
            }
        }
        JLog.e("qqsq x=" + x);
    }

    private void resetImgsTemp() {
        if (imgsTemp == null) {
            imgsTemp = new ArrayList<>();
        } else {
            if (imgsTemp.size() > 0) {
                imgsTemp.clear();
            }
        }
    }

    private boolean setOnlyImage(ImageInfo info) {
        resetImgsTemp();
        LocalMedia lm = getLoMediaFromImage(info);
        if (lm == null) {
            return false;
        }
        imgsTemp.add(lm);
        return true;
    }

    private LocalMedia getLoMediaFromImage(ImageInfo info) {
        if (info == null || TextUtils.isEmpty(info.getImageUrl())) {
            return null;
        }
        String path = UrlUtils.linkUrls(Constances.ST_BASE_URL, info.getImageUrl());
        String type = (path.endsWith(".gif") || path.endsWith(".GIF")) ? "image/gif" : "image";
        JLog.e("rrac  path=" + path + "\n info=" + info);
        LocalMedia lm = new LocalMedia(path, info.getImageTitle(), type);
        return lm;
    }

    private void setImages(List<ImageInfo> images) {
        resetImgsTemp();
        for (int i = 0; i < imageViews.length; i++) {
            if (i <= images.size() - 1) {
                LocalMedia lm = getLoMediaFromImage(images.get(i));
                if (lm != null) {
                    imgsTemp.add(lm);
                }
                final String img = images.get(i).getImageUrl();
                imageViews[i].setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(images.get(i).getImageUrl())) {
                    GlideUtil.loadImgWith11Def(context, img, imageViews[i]);
                }
                imageViews[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.onClick(img);
                        }
                    }
                });
            } else {
                imageViews[i].setVisibility(View.GONE);
            }
        }
    }

    @OnClick({R.id.item_live_room_img1, R.id.item_live_room_img2, R.id.item_live_room_img3})
    public void imgClick1(View view) {
        if (imgsTemp == null || view == null || !(context instanceof Activity)) {
            JLog.e("imgsTemp=" + imgsTemp + "\n  view=" + view + "   context" + content);
            return;
        }
        int pos = (int) view.getTag(R.id.item_news_mode_tag);
        Intent intent = new Intent(context, PictureExternalPreviewActivity.class);
        intent.putExtra(PictureConfig.EXTRA_PREVIEW_SELECT_LIST, imgsTemp);
        intent.putExtra(PictureConfig.EXTRA_POSITION, pos);
        intent.putExtra("type", 2);
        context.startActivity(intent);
    }

    @OnClick({R.id.item_live_room_img4, R.id.item_live_room_img5, R.id.item_live_room_img6})
    public void imgClick2(View view) {
        imgClick1(view);
    }

    @OnClick({R.id.item_live_room_img7, R.id.item_live_room_img8, R.id.item_live_room_img9, R.id.iv_item_live_room_only})
    public void imgClick3(View view) {
        imgClick1(view);
    }

    @OnClick(R.id.iv_item_live_room_single)
    public void singleClick(View view) {
        if (isVideo) {
            return;
        }
        imgClick1(view);
    }

    @OnClick(R.id.item_live_room_play)
    public void playCLick(View view) {
        if (view == playBtn) {
            doInternalClick(ivSingle);
        }
    }
}
